package com.ruoyi.zxjd.work.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.zxjd.work.mapper.WorkHandleMapper;
import com.ruoyi.zxjd.work.domain.WorkHandle;
import com.ruoyi.zxjd.work.service.IWorkHandleService;
import com.ruoyi.common.core.text.Convert;

/**
 * 工作处理Service业务层处理
 * 
 * @author lcc
 * @date 2021-03-08
 */
@Service
public class WorkHandleServiceImpl implements IWorkHandleService 
{
    @Autowired
    private WorkHandleMapper workHandleMapper;

    /**
     * 查询工作处理
     * 
     * @param handleId 工作处理ID
     * @return 工作处理
     */
    @Override
    public WorkHandle selectWorkHandleById(Long handleId)
    {
        return workHandleMapper.selectWorkHandleById(handleId);
    }

    /**
     * 查询工作处理列表
     * 
     * @param workHandle 工作处理
     * @return 工作处理
     */
    @Override
    public List<WorkHandle> selectWorkHandleList(WorkHandle workHandle)
    {
        return workHandleMapper.selectWorkHandleList(workHandle);
    }

    /**
     * 新增工作处理
     * 
     * @param workHandle 工作处理
     * @return 结果
     */
    @Override
    public int insertWorkHandle(WorkHandle workHandle)
    {
        //workHandle.setCreateBy(ShiroUtils.getLoginName());
        workHandle.setCreateTime(DateUtils.getNowDate());
        return workHandleMapper.insertWorkHandle(workHandle);
    }

    /**
     * 修改工作处理
     * 
     * @param workHandle 工作处理
     * @return 结果
     */
    @Override
    public int updateWorkHandle(WorkHandle workHandle)
    {
        //workHandle.setUpdateBy(ShiroUtils.getLoginName());
        workHandle.setUpdateTime(DateUtils.getNowDate());
        return workHandleMapper.updateWorkHandle(workHandle);
    }

    /**
     * 删除工作处理对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteWorkHandleByIds(String ids)
    {
        return workHandleMapper.deleteWorkHandleByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除工作处理信息
     * 
     * @param handleId 工作处理ID
     * @return 结果
     */
    @Override
    public int deleteWorkHandleById(Long handleId)
    {
        return workHandleMapper.deleteWorkHandleById(handleId);
    }

    /**
     * 新增或更新工作处理
     * 
     * @param workHandle 工作处理
     * @param num 工作项调整数
     * @return 结果
     */
    @Override
    public int insertOrUpdateWorkHandle(WorkHandle workHandle, int num) {
        int result = 0;
        List<WorkHandle> list = workHandleMapper.selectWorkHandleList(workHandle);
        if(StringUtils.isEmpty(list)){
            workHandle.setTotal(num);
            workHandle.setUntreatedTotal(num);
            result = insertWorkHandle(workHandle);
        }else{
            WorkHandle newWorkHandle = list.get(0);
            newWorkHandle.setTotal(list.get(0).getTotal() + num);
            newWorkHandle.setUntreatedTotal(list.get(0).getUntreatedTotal() + num);
            result = updateWorkHandle(newWorkHandle);
        }
        return result;
    }
}
